package com.fengwk.support.util;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.fengwk.support.exception.BaseException;
import com.sun.mail.util.MailSSLSocketFactory;

/**
 * java mail 工具
 */
public class MailUtils {

	private MailUtils() {
		throw new BaseException("can not instance " + getClass());
	}
	
	private static Session session;// java mail session
	
	/*
	 * 发送用户
	 */
	private static String sender;// 发送者
	private static String senderEmail;// 发送者邮箱
	private static String senderCode;// 发送者授权码
	
	/*
	 * 配置
	 */
	private static String debugPattern = "false";// 是否开启debug调试
	private static String serviceHost = "smtp.qq.com";// 设置邮件服务器主机名
	private static String serviceCheck = "true";// 发送服务器需要身份验证
	private static String serviceAgreement = "smtp";// 发送邮件协议名称
	
	static {
		sender = "2957844658";
		senderEmail = "2957844658@qq.com";
		senderCode = "jifywlcmodfpdffj";
		
		Properties prop = new Properties();
		//开启debug调试，以便在控制台查看
		prop.setProperty("mail.debug", debugPattern);
		//设置邮件服务器主机名
		prop.setProperty("mail.host", serviceHost);
		//发送服务器需要身份验证
		prop.setProperty("mail.smtp.auth", serviceCheck);
		//发送邮件协议名称
		prop.setProperty("mail.transport.protocol", serviceAgreement);

		//开启SSL加密，否则会失败
		MailSSLSocketFactory sf = null;
		try {
			sf = new MailSSLSocketFactory();
			sf.setTrustAllHosts(true);
		} catch (GeneralSecurityException e) {
			e.printStackTrace();
		}
		prop.put("mail.smtp.ssl.enable", "true");
		prop.put("mail.smtp.ssl.socketFactory", sf);
		
		//创建session
		session = Session.getInstance(prop);
	}
	
	/**
	 * 发送邮件
	 * @param acceptor 邮件接收者
	 * @param title 邮件标题
	 * @param content 邮件内容(html格式)
	 */
	public static void SendEmail(String acceptor, String title, String content) throws Exception {
		//通过session得到transport对象
		Transport ts = session.getTransport();
		//连接邮件服务器：邮箱类型，帐号，授权码
		ts.connect("smtp.qq.com", sender, senderCode);
		//创建邮件
		Message message = createSimpleMail(session, acceptor, title, content);
		//发送邮件
		ts.sendMessage(message, message.getAllRecipients());
		ts.close();
	}
	
	/**
	 * 创建简单的包含文本的邮件
	 * @param session javax.mail.Session对象
	 * @param emailAccepter 邮件接收者
	 * @param emailTitle 邮件标题
	 * @param emailContent 邮件内容(html格式)
	 * @return 包含简单的文本的邮件对象
	 */
	private static MimeMessage createSimpleMail(Session session, String emailAccepter, String emailTitle, String emailContent) throws Exception {
		// 创建邮件对象
		MimeMessage message = new MimeMessage(session);
		try {
			// 指明邮件的发件人
			message.setFrom(new InternetAddress(senderEmail));
			// 指明邮件的收件人，现在发件人和收件人是一样的，那就是自己给自己发
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(emailAccepter));
			// 邮件的标题
			message.setSubject(emailTitle);
			// 邮件的文本内容
			message.setContent(emailContent, "text/html;charset=utf-8");
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("创建简单邮件信息内容失败");
		}
		// 返回创建好的邮件对象
		return message;
	}
	
}
